/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.stress.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.constant.StressConstant;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.stress.entity.Gateway;
import org.springblade.modules.stress.vo.GatewayVO;
import org.springblade.modules.stress.mapper.GatewayMapper;
import org.springblade.modules.stress.service.IGatewayService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 传感器设备管理表 服务实现类
 *
 * @author Yanzujian
 * @since 2021-01-09
 */
@Service
public class GatewayServiceImpl extends BaseServiceImpl<GatewayMapper, Gateway> implements IGatewayService {

	@Override
	public IPage<GatewayVO> selectGatewayPage(IPage<GatewayVO> page, GatewayVO gateway) {
		return page.setRecords(baseMapper.selectGatewayPage(page, gateway));
	}

	@Override
	@Transactional
	public boolean removeGateWay(String ids) {
		List<Gateway> gateways = baseMapper.selectList(Wrappers.<Gateway>query().lambda().eq(Gateway::getSensorType, StressConstant.STRESS_SENSOR_TYPE_ACQUISITION).in(Gateway::getParentId, Func.toLongList(ids)));
		//1、删除传感器
		for (Gateway g: gateways) {
			baseMapper.delete(Wrappers.<Gateway>query().lambda().eq(Gateway::getSensorType, StressConstant.STRESS_SENSOR_TYPE_SENSOR).eq(Gateway::getParentId, g.getId()));
		}
		//2、删除采集仪
		baseMapper.delete(Wrappers.<Gateway>query().lambda().eq(Gateway::getSensorType, StressConstant.STRESS_SENSOR_TYPE_ACQUISITION).in(Gateway::getParentId, Func.toLongList(ids)));
		//3、删除网关
		return removeByIds(Func.toLongList(ids));
	}

}
